package banking6;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:恋恋
 * Date:2022-10-26
 * Time:18:08
 */
public class CheckingAccount extends Account{
    SavingsAccount protectedBy=null;//透支保护

    public CheckingAccount(double balance) {
        super(balance);
    }

    public CheckingAccount(double balance, SavingsAccount protect) {
        super(balance);
        this.protectedBy = protect;
    }

    @Override
    public boolean withdraw(double amt) {
        //当前余额足够弥补取款amt,则正常进行
        if (balance >= amt) {
            balance =balance-amt;
            return true;
        }
        if(protectedBy !=null){//判断这个用户是否有这个SavingsAccount卡
            if (balance < amt && amt-balance < protectedBy.balance) {
                protectedBy.balance = protectedBy.balance +(balance-amt);
                balance = 0;
                return true;
            }
        }

        this.balance = balance;//整个交易失败，但余额未受影响
        return false;


    }
}
